<?php
namespace app\common\extract;

use app\common\extract\Extract;
use PhpOffice\PhpSpreadsheet\IOFactory as IOFactory;
use PhpOffice\PhpSpreadsheet\Reader\Exception as SheetException;

class ExtractExecl extends Extract
{
    /**
     * 
     */
    public function getArray()
    {
        if(!$this->_file){
            return [];
        }
        $file_data = $this->getSheet($this->_file);
        if (count($file_data) == 1 && $file_data[0] == null) {
            $file_data = [];
        }
        return $file_data;
    }

    /**
     *  调用PhpSpreadsheet抽取文件的文字
     */
    protected function getSheet($file)
    {
        try {
            if(!$file)
            {
                throw new \Exception("File is not exist!");
            }
            $double_arr = IOFactory::load($file)->getActiveSheet()->toArray();
            foreach($double_arr as &$arr)
            {
                $arr = $arr[0];
            }
            return $double_arr;
        } catch (SheetException $e) {
            throw new \Exception($e->getMessage());
        }
    }
}